home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Plus 1995 #5 & #6
/
Amiga Plus CD - 1995 - No. 5 and 6.iso
/
pd
/
daten
/
astrolog
/
src
/
xtras.c
< prev
Wrap
C/C++ Source or Header
|
1995-08-11
|
7KB
|
284 lines
/* -*- C -*-
** Extras for ASTROLOG
**
** (c)Copyright 1995 by Tobias Ferber, All Rights Reserved
*/
/* $VER: $Id: xtras.c,v 1.4 1995/06/26 16:13:29 tf Exp $ */
#include "astrolog.h"
#include <stdarg.h>
#include <stdio.h>
static struct { char *s; int c; } ctab[]=
{
{ "{black}", kBlack },
{ "{maroon}", kMaroon },
{ "{dkgreen}", kDkGreen },
{ "{orange}", kOrange },
{ "{dkblue}", kDkBlue },
{ "{purple}", kPurple },
{ "{dkcyan}", kDkCyan },
{ "{ltgray}", kLtGray },
{ "{dkgray}", kDkGray },
{ "{red}", kRed },
{ "{green}", kGreen },
{ "{yellow}", kYellow },
{ "{blue}", kBlue },
{ "{magenta}", kMagenta },
{ "{cyan}", kCyan },
{ "{white}", kWhite },
{ (char *)0, 0 }
};
static int current_line = 1;
static int current_column = 1;
#define XBUF 1024 /* length of line buffers */
/*
** This function is similar to DrawPrint() [see "xcharts0.c"] and
** prints given string `s' to the screen
*/
/****** xtras/xPutString *****************************************************
*
* NAME
* xPutString -- Render text on the graphic screen
*
* SYNOPSIS
* xPutString(str);
*
* void xPutString(char *);
*
* FUNCTION
* This procedure prints given string `str' onto astrolog's graphic chart
* display at the position where the previous call's output ended. I.e.:
* xPutString() has an invisible cursor which is advanced to the end of
* it's output, each time you call this function. The usual "escaped"
* character sequences (like '\n' ot '\b') are handled by this function
* as well.
*
* The text is rendered using the current pen as set via DrawColor().
* However, some special macros can be used to change the color within
* a string. These macros are:
*
* {black} kBlack {dkgray} kDkGray
* {maroon} kMaroon {red} kRed
* {dkgreen} kDkGreen {green} kGreen
* {orange} kOrange {yellow} kYellow
* {dkblue} kDkBlue {blue} kBlue
* {purple} kPurple {magenta} kMagenta
* {dkcyan} kDkCyan {cyan} kCyan
* {ltgray} kLtGray {white} kWhite
*
* INPUTS
* str - The string to render
*
* RESULT
* none
*
* EXAMPLE
* xPrintAt(3,5,""); \* initialize cursor position *\
* xPutSting("This text is rendered using the current color,\n"
* "{white}whereas {red}this {green}changes {purple}it");
*
* NOTES
* xPutString() restores the initial pen if a color macro changes it.
* So xPutString("{red}") is not the same as DrawColor(kRed).
*
* BUGS
* '\t' is not implemented.
*
* SEE ALSO
* xPrint(), xPrintAt()
*
******************************************************************************
*
*
*/
void xPutString(s)
const char *s;
{
char buf[XBUF];
int i,n, pen= gi.kiCur;
#define showit do { buf[n]= '\0'; \
DrawSz( buf, current_column * xFont * gi.nScaleT, \
current_line * yFont * gi.nScaleT, dtLeft | dtBottom ); } while(0)
for(n=0; *s; s++)
{
switch(*s)
{
case '\n':
if(n>0) { showit; n=0; }
current_line++;
current_column= 1;
break;
case '\b':
if(n>0) { showit; current_column+=n; n=0; }
if(current_column > 1)
current_column--;
break;
case '\r':
if(n>0) { showit; n=0; }
current_line++;
break;
case '{':
{
char *a, *b;
for(i=0; ctab[i].s; i++)
{
a= s; b= ctab[i].s;
while(*a && *b && *a==*b) { ++a; ++b; }
if(*b=='\0')
break;
}
if(b && *b=='\0') /* we matched a color */
{
if(n>0) { showit; current_column+=n; n=0; }
DrawColor(ctab[i].c);
s= --a;
break;
}
else buf[n++]= *s; /* ordinary brace */
}
break;
default:
buf[n++]= *s;
break;
}
}
if(n>0) { showit; current_column+=n; n=0; }
#undef showit
gi.kiCur= pen;
}
/****** xtras/xPrint *********************************************************
*
* NAME
* xPrint -- Render a format string to astrolog's graphic screen
*
* SYNOPSIS
* xPrint(fmt,...);
*
* void xPrint(char *,...);
*
* FUNCTION
* This procedure does basically the same as xPutString(), i.e. it
* renders a string to the graphic screen, using astrolog's internal
* font. The difference between xPutString() and xPrint() is that
* xPrint() performs printf() like substitutions in the given
* format string `fmt'.
*
* INPUTS
* fmt - A printf() like format string and
* ... it's arguments
*
* RESULT
* none
*
* EXAMPLE
*
* NOTES
*
* BUGS
*
* SEE ALSO
* xPutString(), xPrintAt()
*
******************************************************************************
*
*
*/
void xPrint(fmt, ...)
const char *fmt;
{
char *buf[XBUF];
va_list argp;
va_start(argp,fmt);
vsprintf(buf,(char *)fmt,argp);
xPutString(buf);
va_end(argp);
}
/****** xtras/xPrintAt *******************************************************
*
* NAME
* xPrintAt -- Render format string at given position on the screen
*
* SYNOPSIS
* xPrintAt(line,column, fmt,...);
*
* void xPrintAt(int,int, char *,...);
*
* FUNCTION
* This procedure moves an invisible cursor to given location
* (line,column) on astrolog's screen and renders the format string
* `fmt' at this location via xPutString() after expanding it using
* the given arguments `...' (if present).
*
* INPUTS
* line,column - The position where the first character shall be
* rendered on the screen
* fmt - A printf() like format string and
* ... it's arguments
*
* RESULT
* none
*
* EXAMPLE
* xPrintAt(5,3,"{green}I compute {white}1+1={red}%d{green}.",1+1);
*
* NOTES
* You should *always* use xPrintAt() before using either xPrint()
* or xPutString() in order to initialize the cursor position.
*
* BUGS
*
* SEE ALSO
* xPutString(), xPrint()
*
******************************************************************************
*
*
*/
void xPrintAt(line,column, fmt, ...)
int line,column;
const char *fmt;
{
char *buf[XBUF];
va_list argp;
va_start(argp,fmt);
vsprintf(buf,(char *)fmt,argp);
if(line > 0) current_line = line;
else current_line = 1;
if(column > 0) current_column = column;
else current_column = 1;
xPutString(buf);
va_end(argp);
}